﻿//说明文件没有找到，谁给删除了？！


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;


namespace OF.Component.DAL
{
    ///<summary>
    ///UserInfor 数据访问层For SqlServer 
    ///</summary>
    internal partial class UserInfor : IDAL.IUserInfor
    {
        /// <summary>
        /// 根据商家Id取出用户信息
        /// </summary>
        /// <param name="nDealerId"></param>
        /// <returns></returns>
        public UserInforInfo GetByDealerId(int nDealerId)
        {
            Model.UserInforInfo info = null;
            string strSql = @"	SELECT * FROM UserInfor WHERE  DealerId=@UserID
";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@UserID", DbType.Int32, nDealerId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        public UserInforInfo GetByMobile(string mobile)
        {
            UserInforInfo info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(@"select * FROM  UserInfor where Mobile=@Mobile");
            db.AddInParameter(dbc, "@Mobile", DbType.String, mobile);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        public UserInforInfo GetByWxOpenId(string wxOpenId)
        {
            UserInforInfo info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(@"select * FROM  UserInfor where WxOpenID=@WxOpenID");
            db.AddInParameter(dbc, "@WxOpenID", DbType.String, wxOpenId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
        }

        public int getNextUserID()
        {
            int nReturn = 0;
            string strSql = "SELECT ISNULL(MAX(UserID),0)+1 FROM UserInfor";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            try
            {
                object o = db.ExecuteScalar(dbc);
                if (o!=null)
                {
                    nReturn = Convert.ToInt32(o);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nReturn;
        }

    }
}